using System;
using System.Collections.Generic;
using System.Xml;
namespace Microsoft.Phone.Controls.Maps.Core
{
	internal static class TfeCoverageMapParser
	{
		public static bool Parse(XmlReader reader, List<TfeZoomLevelRange> maps, out string mapGeneration, out int minTfeZoomLevel, out int maxTfeZoomLevel)
		{
			bool result = false;
			mapGeneration = null;
			minTfeZoomLevel = -1;
			maxTfeZoomLevel = -1;
			try
			{
				result = TfeCoverageMapParser.ParseXml(reader, maps, out mapGeneration, out minTfeZoomLevel, out maxTfeZoomLevel);
			}
			catch (XmlException)
			{
			}
			return result;
		}
		private static bool ParseXml(XmlReader xml, List<TfeZoomLevelRange> maps, out string mapGeneration, out int minTfeZoomLevel, out int maxTfeZoomLevel)
		{
			bool flag = true;
			mapGeneration = "1";
			minTfeZoomLevel = 2147483647;
			maxTfeZoomLevel = -2147483648;
			TfeZoomLevelRange tfeZoomLevelRange = null;
			while (xml.Read() && flag)
			{
				XmlNodeType nodeType = xml.get_NodeType();
				if (nodeType != 1)
				{
					if (nodeType == 15)
					{
						string name;
						if ((name = xml.get_Name()) != null && name == "ZoomLevelRange")
						{
							maps.Add(tfeZoomLevelRange);
							if (tfeZoomLevelRange.MinimumZoom < minTfeZoomLevel)
							{
								minTfeZoomLevel = tfeZoomLevelRange.MinimumZoom;
							}
							if (tfeZoomLevelRange.MaximumZoom > maxTfeZoomLevel)
							{
								maxTfeZoomLevel = tfeZoomLevelRange.MaximumZoom;
							}
							tfeZoomLevelRange = null;
						}
					}
				}
				else
				{
					string name2;
					if ((name2 = xml.get_Name()) != null)
					{
						if (!(name2 == "MapGeneration"))
						{
							if (!(name2 == "ZoomLevelRange"))
							{
								if (name2 == "R")
								{
									flag = TfeCoverageMapParser.ParseRegion(xml, tfeZoomLevelRange);
								}
							}
							else
							{
								flag = TfeCoverageMapParser.ParseZoomLevelRange(xml, out tfeZoomLevelRange);
							}
						}
						else
						{
							mapGeneration = xml.ReadElementContentAsString();
						}
					}
				}
			}
			return flag;
		}
		private static bool ParseZoomLevelRange(XmlReader xml, out TfeZoomLevelRange currentMap)
		{
			bool result = true;
			int num = 0;
			int num2 = 0;
			currentMap = null;
			while (xml.MoveToNextAttribute())
			{
				string name;
				if ((name = xml.get_Name()) != null)
				{
					if (!(name == "min"))
					{
						if (name == "max")
						{
							num2 = xml.ReadContentAsInt();
						}
					}
					else
					{
						num = xml.ReadContentAsInt();
					}
				}
			}
			if (num != 0 && num2 != 0)
			{
				currentMap = new TfeZoomLevelRange((byte)num, (byte)num2);
			}
			else
			{
				result = false;
			}
			return result;
		}
		private static bool ParseRegion(XmlReader xml, TfeZoomLevelRange currentMap)
		{
			bool result = false;
			string text = null;
			int num = 0;
			if (currentMap != null)
			{
				while (xml.MoveToNextAttribute())
				{
					string name;
					if ((name = xml.get_Name()) != null)
					{
						if (!(name == "c"))
						{
							if (name == "g")
							{
								num = xml.ReadContentAsInt();
							}
						}
						else
						{
							text = xml.ReadContentAsString();
						}
					}
				}
				if (text != null && num != 0)
				{
					result = true;
					currentMap.AddRegion(text, num);
				}
			}
			return result;
		}
	}
}
